package com.jzp.gulimail.product.service.impl;

import com.jzp.common.utils.Query;
import com.jzp.gulimail.product.entity.PmsSkuImagesEntity;
import com.jzp.gulimail.product.entity.PmsSpuInfoDescEntity;
import com.jzp.gulimail.product.service.*;
import com.jzp.gulimail.product.vo.SkuItemSaleAttrVo;
import com.jzp.gulimail.product.vo.SkuItemVo;
import com.jzp.gulimail.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzp.common.utils.PageUtils;

import com.jzp.gulimail.product.dao.PmsSkuInfoDao;
import com.jzp.gulimail.product.entity.PmsSkuInfoEntity;


@Service("pmsSkuInfoService")
public class PmsSkuInfoServiceImpl extends ServiceImpl<PmsSkuInfoDao, PmsSkuInfoEntity> implements PmsSkuInfoService {

    @Autowired
    PmsSkuImagesService imagesService;
    @Autowired
    PmsSpuInfoDescService spuInfoDescService;
    @Autowired
    PmsAttrGroupService attrGroupService;
    @Autowired
    PmsSkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsSkuInfoEntity> page = this.page(
                new Query<PmsSkuInfoEntity>().getPage(params),
                new QueryWrapper<PmsSkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(PmsSkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<PmsSkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key=(String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper)->{
                wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        String catelogId=(String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            queryWrapper.eq("catalog_id",catelogId);
        }
        String brandId=(String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(catelogId)){
            queryWrapper.eq("brand_id",brandId);
        }
        String min=(String) params.get("min");
        if(!StringUtils.isEmpty(min)){
            queryWrapper.ge("price",min);
        }
        String max=(String) params.get("max");
        if(!StringUtils.isEmpty(max)){
           try {
               BigDecimal bigDecimal=new BigDecimal(max);
               if (bigDecimal.compareTo(new BigDecimal("0"))==1){
                   queryWrapper.le("price",max);
               }
           }catch (Exception e){

           }
        }
        IPage<PmsSkuInfoEntity> page = this.page(
                new Query<PmsSkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);

    }

    @Override
    public List<PmsSkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<PmsSkuInfoEntity> list=this.list(new QueryWrapper<PmsSkuInfoEntity>().eq("spu_id",spuId));
        return list;
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<PmsSkuInfoEntity> infoFuture=CompletableFuture.supplyAsync(()->{
            //1.sku基本信息获取 pms_sku_info
            PmsSkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        },executor);

        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            //3.获取spu的销售属性组合。
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(res -> {
            //4.获取spu的介绍 pms_spu_info_desc
            PmsSpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesp(spuInfoDescEntity);
        }, executor);

        CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync(res -> {
            //5.获取spu的规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);


        //2.sku的图片信息 pms_sku_images
        CompletableFuture<Void> imageFuture=CompletableFuture.runAsync(()->{
            List<PmsSkuImagesEntity> images=imagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(images);
        },executor);

        //等到所有任务都完成
        CompletableFuture.allOf(saleAttrFuture,descFuture,baseAttrFuture,imageFuture).get();

        return skuItemVo;

    }

}